package com.ym.controller.doctor.work;

import com.ym.base.enums.FlagEnum;
import com.ym.common.constant.Constants;
import com.ym.core.annotation.ApiSign;
import com.ym.core.annotation.Log;
import com.ym.core.config.ConfigCenterProperties;
import com.ym.core.config.QiNiuYunConfig;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.shiro.ShiroUtil;
import com.ym.medical.domain.param.CancelOrderParam;
import com.ym.medical.domain.vo.OrderAndFileVO;
import com.ym.medical.domain.vo.RecordAndTimeVo;
import com.ym.medical.service.IDoctorSV;
import com.ym.medical.service.IInquiryOrderSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 张文博
 * @version: 2019年06月18日 11:30
 */
@Api("电话预约回显")
@Controller
@ApiSign
@RequestMapping(value = "/doctor/work/order",headers = Constants.Api.VERSION)
public class GraphicPhoneOrderController extends BaseController {

	@Autowired
	private IInquiryOrderSV iInquiryOrderSV;

	@Autowired
	private IDoctorSV iDoctorSV;

	@Autowired
	private ConfigCenterProperties configCenterProperties;

	@Log(dataBase = false, printBody = false)
	@ApiOperation("电话图文预约数据回显")
	@GetMapping(value = "/orderEcho")
	@ResponseBody
	public APIResponse<List<OrderAndFileVO>> selectOrderEcho(
			@RequestParam(value = "ioId") Long ioId) throws Exception {
		QiNiuYunConfig qiNiuYunConfig = configCenterProperties.getQiNiuYunConfig();
		Long currId = ShiroUtil.getLoginId();
		//Long currId = 13L;
		String userType = ShiroUtil.getLoginUser().getUserTypeEnum().getKey();
		//String userType = "01";
		List<OrderAndFileVO> order = iInquiryOrderSV.selectOrderEcho(currId,userType,ioId);
		List<RecordAndTimeVo> realUrl = order.get(0).getRecordUrl();
		List<RecordAndTimeVo> list = new ArrayList<>();
		String uri = qiNiuYunConfig.getShowRecUrl();
		int size = realUrl.size();
		if (!order.isEmpty() && size == 1) {
			realUrl.get(0).setFileInfoId(
					uri + realUrl.get(0).getFileInfoId());
			order.get(0).setRecordUrl(realUrl);
		} else if (!order.isEmpty() && realUrl.size() > 1) {
			for (int i = 0; i < realUrl.size(); i++) {
				realUrl.get(i).setFileInfoId(uri + realUrl.get(i).getFileInfoId());
				list.add(realUrl.get(i));
			}
			order.get(0).setRecordUrl(list);
		}
		return successData(order);
	}

	@Log
	@ApiOperation("绑定电话")
	@PostMapping(value = "/axbBind")
	@ResponseBody
	public APIResponse axbBind(
			@ApiParam(name = "orderAndFileVO", value = "电话信息", required = true)
			@RequestBody OrderAndFileVO orderAndFileVO) throws Exception {
		Long doctId = ShiroUtil.getLoginId();
		orderAndFileVO.setDoctId(doctId);
		String relationNum = iDoctorSV.axbBindNumber02(orderAndFileVO);
		if (FlagEnum.IS.getKey().equals(relationNum)) {
			APIResponse.successWithNoData("请稍等片刻喔<^0^>");
		}
		return APIResponse.success("绑定号码", relationNum);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("取消电话订单")
	@PostMapping(value = "/cancelOrder")
	@ResponseBody
	public APIResponse cancelOrder(
			@ApiParam(name = "CancelOrderVo", value = "电话信息", required = true)
			@RequestBody @Validated CancelOrderParam cancelOrderParam) throws Exception {
		int rows = iInquiryOrderSV.cancelOrder(cancelOrderParam);
		if (rows != 1) {
			return successMsg("取消失败");
		}
		return successMsg("取消成功");
	}

}
